package cn.stylefeng.guns.modular.hp.controller;

import cn.stylefeng.guns.config.properties.HpProperties;
import cn.stylefeng.guns.core.common.annotion.BussinessLog;
import cn.stylefeng.guns.core.common.exception.BizExceptionEnum;
import cn.stylefeng.guns.core.common.node.ZTreeNode4Hp;
import cn.stylefeng.guns.core.log.LogObjectHolder;
import cn.stylefeng.guns.modular.hp.constant.ArticleEnum;
import cn.stylefeng.guns.modular.hp.domain.DepartmentExpertLinkDO;
import cn.stylefeng.guns.modular.hp.model.ArticleInfo;
import cn.stylefeng.guns.modular.hp.model.ExpertDepartmentLinkInfo;
import cn.stylefeng.guns.modular.hp.model.ExpertInfo;
import cn.stylefeng.guns.modular.hp.service.IArticleInfoService;
import cn.stylefeng.guns.modular.hp.service.IExpertDepartmentLinkInfoService;
import cn.stylefeng.guns.modular.hp.service.IExpertInfoService;
import cn.stylefeng.guns.modular.hp.warpper.ArticleInfoWrapper;
import cn.stylefeng.guns.modular.hp.warpper.ExpertInfoWrapper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 科室信息控制器
 *
 * @author xzqiang
 * @date 2019-6-10
 */
@Controller
@RequestMapping("/hp/department")
public class DepartmentInfoController extends BaseController {

    private String PREFIX = "/hp/departmentLink/";
    
    @Autowired
    private HpProperties hpProperties;

    @Autowired
	private IExpertDepartmentLinkInfoService expertDepartmentLinkInfoService;

    @Autowired
	private IArticleInfoService articleInfoService;

    @Autowired
    private IExpertInfoService expertInfoService;

    /**
     *
     */
    @RequestMapping("")
    public String index() {
    	String htmlPath = "departmentLink.html";
        return PREFIX + htmlPath;
    }


    /**
     * 获取科室列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list() {
        List<Map<String, Object>> list = this.articleInfoService.list(ArticleEnum.DEPARTMENT, null);
        return super.warpObject(new ArticleInfoWrapper(list));
    }

    /**
     * 获取科室列表
     */
    @RequestMapping(value = "/expertlist/{departmentInfoId}")
    @ResponseBody
    public Object expertlist(@PathVariable String departmentInfoId) throws Exception {
        List<ExpertInfo> list = this.expertInfoService.departmentExpertList(departmentInfoId);
        List<Map<String, Object>> mapList = new ArrayList<>();
        for(ExpertInfo info : list){
            mapList.add(Obj2Map(info));
        }
        return super.warpObject(new ExpertInfoWrapper(mapList));
    }

    /**
     * 获取角色科室列表
     */
    @RequestMapping(value = "/departmentTreeListByExpertId/{expertId}")
    @ResponseBody
    public List<ZTreeNode4Hp> departmentTreeListByExpertId(@PathVariable String expertId) {
        ExpertInfo expertInfo = this.expertInfoService.selectById(expertId);
        List<ExpertDepartmentLinkInfo> departmentList = this.expertDepartmentLinkInfoService.getLinkInfoByExpertInfoId(expertId);
        if (ToolUtil.isEmpty(departmentList)) {
            return this.articleInfoService.departmentTreeList();
        } else {
            return this.articleInfoService.departmentTreeListById(departmentList);
        }
    }

    /**
     * 配置权限
     */
    @RequestMapping("/setDepartmentLink")
    @ResponseBody
    public ResponseData setDepartmentLink(@RequestParam("departmentIds") String departmentIds, @RequestParam("expertInfoId") String expertInfoId) {
        if (ToolUtil.isOneEmpty(departmentIds, expertInfoId)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        this.expertDepartmentLinkInfoService.setDepartmentLink(departmentIds, expertInfoId);
        return SUCCESS_TIP;
    }

    /**
     * 跳转到修改信息详情
     */
    @RequestMapping("/info_update/{departmentInfoId}/{expertInfoId}")
    public String infoUpdate(@PathVariable String departmentInfoId, @PathVariable String expertInfoId, Model model) {
        ExpertDepartmentLinkInfo linkInfo = this.expertDepartmentLinkInfoService.getLinkInfoByDepartmentInfoIdAndExpertInfoId(departmentInfoId,expertInfoId);

        ExpertInfo expertInfo = this.expertInfoService.selectById(expertInfoId);
        ArticleInfo departInfo = this.articleInfoService.selectById(departmentInfoId);


        DepartmentExpertLinkDO infoDO = new DepartmentExpertLinkDO(linkInfo.getId(), departInfo.getTitle(), expertInfo.getName(), linkInfo.getPri());

        model.addAttribute("info", infoDO);
        LogObjectHolder.me().set(infoDO);

        String htmlPath = "departmentLink_edit.html";
        return PREFIX + htmlPath;
    }

    /**
     * 修改信息
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @BussinessLog(value = "修改信息")
    public Object update(DepartmentExpertLinkDO departmentExpertLinkDO) {
        if (ToolUtil.isOneEmpty(departmentExpertLinkDO, departmentExpertLinkDO.getId(), departmentExpertLinkDO.getPri())) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        //只更新排序
        ExpertDepartmentLinkInfo linkInfo = this.expertDepartmentLinkInfoService.selectById(departmentExpertLinkDO.getId());
        linkInfo.setPri(departmentExpertLinkDO.getPri());
        linkInfo.updateById();
        return SUCCESS_TIP;
    }

    /**
     * 删除信息
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @BussinessLog(value = "删除信息")
    public Object delete(@RequestParam String departmentInfoId,@RequestParam String expertInfoId) {

        this.expertDepartmentLinkInfoService.deleteByDepartmentInfoIdAndExpertInfoId(departmentInfoId, expertInfoId);

        return SUCCESS_TIP;
    }

    private Map<String,Object> Obj2Map(Object obj) throws Exception{
        Map<String,Object> map=new HashMap<>();
        Field[] fields = obj.getClass().getDeclaredFields();
        for(Field field:fields){
            field.setAccessible(true);
            map.put(field.getName(), field.get(obj));
        }
        return map;
    }
}
